package com.jdew.thkj.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jdew.thkj.R;
import com.jdew.thkj.bean.HwBean;
import com.jdew.thkj.onitemInterface.OnItemClickListener;

import java.text.DecimalFormat;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by yangyang on 2017/9/18.
 */

public class HwAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {
    private List<HwBean.DataBean.PageListBean> dataList;
    private LayoutInflater layoutInflater;
    private Context context;
    private View itemView;
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;
    private OnItemClickListener mItemClickListener;
    private String maType;

    @Override
    public void onClick(View v) {
        if (mItemClickListener!=null){
            mItemClickListener.onItemClick((Integer) v.getTag());
        }
    }

    public void setItemClickListener(OnItemClickListener itemClickListener) {
        mItemClickListener = itemClickListener;
    }
    public HwAdapter(Context context, List<HwBean.DataBean.PageListBean> dataList) {
        this.context=context;
        this.dataList = dataList;
        layoutInflater = LayoutInflater.from(context);
        sp = context.getSharedPreferences( "user", Activity.MODE_PRIVATE );
        editor = sp.edit();
        maType=sp.getString("SpareTwo","");
    }
    public void setDataList(List<HwBean.DataBean.PageListBean> dataList){
        this.dataList=dataList;
        notifyDataSetChanged();
    }
    public void setDataAllList(List<HwBean.DataBean.PageListBean> dataList){
        this.dataList.addAll(dataList);
        notifyDataSetChanged();
    }
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
         itemView = layoutInflater.inflate(R.layout.product_list_item, parent, false);
         itemView.setOnClickListener(this);
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        MyViewHolder myviewHolder=(MyViewHolder)holder;
        myviewHolder.BindView(holder,position);
        myviewHolder.itemView.setTag(position);
    }
    @Override
    public int getItemCount() {
        return dataList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.product)
        ImageView product;
        @Bind(R.id.name)
        TextView name;
        @Bind(R.id.price)
        TextView price;
        @Bind(R.id.before_price)
        TextView beforePrice;
        @Bind(R.id.quan)
        TextView quan;
        @Bind(R.id.earn)
        TextView earn;
        @Bind(R.id.num)
        TextView num;
        @Bind(R.id.source)
        ImageView source;

        MyViewHolder(View itemView) {
            super( itemView );
            ButterKnife.bind( this, itemView );
        }

        public void BindView(RecyclerView.ViewHolder holder, int position) {
                 DecimalFormat df = new DecimalFormat( "0.00" );
                ((MyViewHolder) holder).beforePrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
                if(dataList.get( position ).getUserType()==0){
                    source.setImageResource(R.mipmap.tb);
                }else{
                    source.setImageResource(R.mipmap.tm);
                   }
                ((MyViewHolder) holder).beforePrice.setText("原价"+df.format( dataList.get( position ).getZkPrice()));
                ((MyViewHolder) holder).name.setText( dataList.get( position ).getTitle().replace("<span class=H>","").replace("</span>",""));
                ((MyViewHolder) holder).num.setText( "月销:" + dataList.get( position ).getBiz30day() );
                ((MyViewHolder) holder).price.setText( df.format( dataList.get( position ).getZkPrice() - dataList.get( position ).getCouponAmount()));
                dataList.get(position).setShowPrice(price.getText().toString());
                Double high=dataList.get(position).getEventRate()> dataList.get(position).getTkRate()?dataList.get(position).getEventRate():dataList.get(position).getTkRate();
                 if(dataList.get( position ).getCouponAmount()==0){
                     ((MyViewHolder) holder).quan.setText( "0元券" );
//                     ((MyViewHolder) holder).quan.setVisibility(View.INVISIBLE);
                 }else{
//                     ((MyViewHolder) holder).quan.setVisibility(View.VISIBLE);
                     ((MyViewHolder) holder).quan.setText( dataList.get( position ).getCouponAmount() + "元券" );
                 }
            if(maType.equals("")|| maType.equals("1")){
                earn.setVisibility(View.GONE);
            }else{
                earn.setVisibility(View.VISIBLE);
                if(maType.equals("0")){
                    ((MyViewHolder) holder).earn.setText("分享赚" + Double.parseDouble(df.format(Double.parseDouble(price.getText().toString())*high/100)) + "元");
                }else{
                    ((MyViewHolder) holder).earn.setText("分享赚" + Double.parseDouble(df.format(Double.parseDouble(price.getText().toString())*high/100))/2 + "元");
                }
            }
                Glide.with( context ).load( "http:" + dataList.get( position ).getPictUrl()).into( product );
        }
    }

}

